package com.zzyl.serve.service.impl;

import java.time.Duration;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.vo.NursingLevelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.stream.Collectors;

import static com.zzyl.common.constant.CacheConstants.NURSING_LEVEL_ALL_KEY;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private INursingPlanService nursingPlanService;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVO> selectNursingLevelList(NursingLevel nursingLevel) {
        //查询护理等级基本信息
        List<NursingLevel> levels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        //查询对应的护理计划的名称并赋值进去
        if (ObjectUtil.isNotEmpty(levels)) {
            List<NursingLevelVO> voList = levels.stream().map(level -> {
                NursingLevelVO nursingLevelVO = new NursingLevelVO();
                NursingPlan plan = this.nursingPlanService.getById(level.getLplanId());
                BeanUtils.copyProperties(level, nursingLevelVO);
                nursingLevelVO.setPlanName(plan.getPlanName());
                return nursingLevelVO;
            }).collect(Collectors.toList());
            return voList;
        }
        return null;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        redisTemplate.delete(NURSING_LEVEL_ALL_KEY);
        return save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        redisTemplate.delete(NURSING_LEVEL_ALL_KEY);
        return updateById(nursingLevel) ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        redisTemplate.delete(NURSING_LEVEL_ALL_KEY);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        redisTemplate.delete(NURSING_LEVEL_ALL_KEY);
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理等级信息
     *
     * @return
     */
    @Override
    public List<NursingLevel> selectAll() {
        //1.查询缓存中是否带有数据
        List<NursingLevel> levels = (List<NursingLevel>) redisTemplate.opsForValue().get(NURSING_LEVEL_ALL_KEY);
        //2.判断查询的结果是否为空
        if (ObjectUtil.isNotNull(levels)) {
            //代表缓存中有数据,直接返回即可
            return levels;
        }
        //没有,则先从数据库中查找数据
        List<NursingLevel> levelList = this.lambdaQuery().eq(NursingLevel::getStatus, 1)
                .list();
        //再给缓存添加数据
        redisTemplate.opsForValue().set(NURSING_LEVEL_ALL_KEY, levelList, Duration.ofHours(12));
        //在返回即可
        return levelList;
    }
}
